package com.app.teaware.basis;

import android.app.Notification;
import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.multidex.MultiDex;
import androidx.multidex.MultiDexApplication;

import com.app.teaware.R;
import com.bumptech.glide.Glide;
import com.app.teaware.util.update.UpdateUtil;
import com.flyco.animation.BaseAnimatorSet;
import com.flyco.animation.BounceEnter.BounceBottomEnter;
import com.flyco.animation.SlideExit.SlideBottomExit;
import com.hikvision.open.hikvideoplayer.HikVideoPlayerFactory;
import com.lzf.easyfloat.EasyFloat;
import com.lzy.imagepicker.ImagePicker;
import com.tencent.smtt.sdk.QbSdk;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;
import com.zhy.http.okhttp.OkHttpUtils;

import java.io.File;

import cn.jiguang.analytics.android.api.JAnalyticsInterface;
import cn.jpush.android.api.BasicPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface;
import my.LogUtil;
import my.http.MyInterceptor;
import my.http.TokenInterceptor;
import okhttp3.OkHttpClient;

public class BasisApp extends MultiDexApplication {
    private static BasisApp APP;

    public static Context mContext;
    private static Handler handler;
    public static String DIR_PATH_USER;

    public static boolean isProgramExit = false;
    static Toast mToast;

    public static BaseAnimatorSet bas_in = new BounceBottomEnter();
    public static BaseAnimatorSet bas_out = new SlideBottomExit();

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getBaseContext();
//		mContext = getApplicationContext();
        isProgramExit = false;
        APP = this;
//		SDKInitializer.initialize(this);
        handler = new Handler();
        DIR_PATH_USER = getFilesDir().getAbsolutePath() + "/";
        initX5();
        initJpush();
        initOkHttp();

        initImagePicker();

        ZXingLibrary.initDisplayOpinion(this);
        UpdateUtil.initXUpdate(this);


        UpdateUtil.initXUpdate(this);
        EasyFloat.init(this, Constants.DEBUG);
        HikVideoPlayerFactory.initLib("", true);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }

    private void initJpush() {
        JPushInterface.init(this);     		// 初始化 JPush
        JPushInterface.setDebugMode(Constants.DEBUG); 	// 设置开启日志,发布时请关闭日志
        BasicPushNotificationBuilder builderB = new BasicPushNotificationBuilder(this);
        builderB.statusBarDrawable = R.drawable.icon_notification_small;
        builderB.notificationFlags = Notification.FLAG_AUTO_CANCEL
                | Notification.FLAG_SHOW_LIGHTS;  //设置为自动消失和呼吸灯闪烁
        builderB.notificationDefaults = Notification.DEFAULT_SOUND
                | Notification.DEFAULT_VIBRATE
                | Notification.DEFAULT_LIGHTS;  // 设置为铃声、震动、呼吸灯闪烁都要
        JPushInterface.setPushNotificationBuilder(1, builderB);

//        CustomPushNotificationBuilder builder = new
//                CustomPushNotificationBuilder(this,
//                R.layout.customer_notitfication_layout,
//                R.id.icon,
//                R.id.title,
//                R.id.text);
//        // 指定定制的 Notification Layout
//        builder.statusBarDrawable = R.drawable.icon_notification_small;
//        // 指定最顶层状态栏小图标
//        builder.layoutIconDrawable = R.drawable.icon_notification_small;
//        // 指定下拉状态栏时显示的通知图标
//        JPushInterface.setPushNotificationBuilder(2, builder);


        //极光统计
        JAnalyticsInterface.init(this);
        JAnalyticsInterface.setDebugMode(Constants.DEBUG);
        JAnalyticsInterface.initCrashHandler(this);

//       String registrationId = JPushInterface.getRegistrationID(this);
//        LogUtil.i("run:--------->registrationId： "+registrationId );
    }

    public static BasisApp getInstance() {
        return APP;
    }

    private void initOkHttp(){

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(new MyInterceptor())
                .addInterceptor(new TokenInterceptor())
                //其他配置
                .build();
        OkHttpUtils.initClient(okHttpClient);
    }

    /**
     * 短消息提示
     */
    public static void showToast(final int resId) {
        handler.post(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                if (mToast != null) {
                    mToast.cancel();
                }
                mToast = Toast.makeText(mContext, resId, Toast.LENGTH_SHORT);
                mToast.show();
            }
        });
    }

    /**
     * 短消息提示
     */
    public static void showToast(final String text) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
//				if (mToast != null) {
//					mToast.cancel();
//				}
                mToast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT);
                mToast.show();
            }
        });
    }

    void initImagePicker() {
        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoaderForimgpickup());   //设置图片加载器
        imagePicker.setShowCamera(true);  //显示拍照按钮
        imagePicker.setCrop(false);        //允许裁剪（单选才有效）
//        imagePicker.setSaveRectangle(false); //是否按矩形区域保存
        imagePicker.setSelectLimit(9);    //选中数量限制
//        imagePicker.setStyle(CropImageView.Style.RECTANGLE);  //裁剪框的形状
        imagePicker.setFocusWidth(800);   //裁剪框的宽度。单位像素（圆形自动取宽高最小值）
        imagePicker.setFocusHeight(800);  //裁剪框的高度。单位像素（圆形自动取宽高最小值）
        imagePicker.setOutPutX(1000);//保存文件的宽度。单位像素
        imagePicker.setOutPutY(1000);//保存文件的高度。单位像素
    }


    public static void loadImg(Context mContext, String imgurl, ImageView imageView, int res_default) {

		if(TextUtils.isEmpty(imgurl)){
            imageView.setImageResource(res_default);
		    return;
        };
//        LogUtil.i("loadImg--->"+imgurl);
        if(TextUtils.isEmpty(imgurl)){

        }else if(!imgurl.contains("://")){
            Glide.with(mContext).load(new File(imgurl))
                    .placeholder(res_default).error(res_default)
                    .into(imageView);
        }else {
            Glide.with(mContext).load(imgurl)
//				.apply(new RequestOptions().placeholder(res_default).error(res_default))
                    .placeholder(res_default).error(res_default)
                    .into(imageView);
        }

    }

    public static void loadImg(Context mContext, String imgurl, ImageView imageView) {
        loadImg(mContext, imgurl, imageView, R.drawable.base_transparent);
    }

    public static void loadImg(String imgurl, ImageView imageView) {
        loadImg(BasisApp.mContext, imgurl, imageView, R.drawable.base_transparent);
    }

    public static void loadImgAsbitmap(Context mContext, String imgurl, ImageView imageView, int res_default) {

//		if(TextUtil.isEmpty(imgurl))return;
        Glide.with(mContext)
//				.asBitmap()
                .asBitmap()
                .load(imgurl)
                .placeholder(res_default)
//				.apply(new RequestOptions().placeholder(res_default).error(res_default))
                .into(imageView);
    }

    private void initX5() {
        QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {
            @Override
            public void onViewInitFinished(boolean arg0) {
                LogUtil.d("app", " onViewInitFinished is " + arg0);
            }

            @Override
            public void onCoreInitFinished() {
            }
        };
//		QbSdk.allowThirdPartyAppDownload(true);
        QbSdk.initX5Environment(getApplicationContext(), cb);
    }

}
